package cn.ym.mall.service.impl;

import cn.ym.mall.dao.BrandDao;
import cn.ym.mall.dao.impl.BrandDaoImpl;
import cn.ym.mall.entity.Brand;
import cn.ym.mall.service.BrandService;
import cn.ym.mall.utils.DruidUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BrandServiceImpl implements BrandService {
    BrandDao brandDao = new BrandDaoImpl();

    /**
     * 查询所有品牌
     *
     * @return
     */
    @Override
    public List<Brand> getAll() throws SQLException {
        Connection conn = null;
        List<Brand> brandList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection();
            brandList = brandDao.getAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return brandList;
    }

    /**
     * 根据id查询品牌
     *
     * @param bid
     * @return
     */
    @Override
    public Brand getBrandByBid(Integer bid) throws SQLException {
        Connection conn = null;
        Brand brand = new Brand();
        try {
            conn = DruidUtils.getConnection();
            brand = brandDao.getBrandByBid(bid);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return brand;
    }

    /**
     * 添加品牌
     *
     * @param brand
     * @return
     */
    @Override
    public boolean add(Brand brand) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (brandDao.add(brand) == 1) {
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 修改品牌
     *
     * @param brand
     * @return
     */
    @Override
    public boolean update(Brand brand) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (brandDao.update(brand) == 1) {
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 删除品牌
     *
     * @param bid
     * @return
     */
    @Override
    public boolean delete(Integer bid) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (brandDao.delete(bid) == 1) {
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return false;
    }
}
